//
//  BottomBarController.m
//  healthApp
//
//  Created by william mu on 12-8-28.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "BottomBarController.h"
#import "Common.h"

@interface BottomBarController ()

@end

@implementation BottomBarController
@synthesize kun;
@synthesize fa;
@synthesize yun;
@synthesize men;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    CGFloat height = self.view.frame.size.width;
    self.view.frame = CGRectMake(0, 480 - 44, 320, 44);
    
    [self initBtn];
}

-(void) initBtn
{
    [self setBtnChecked:self.kun checked:NO];
    [self setBtnChecked:self.fa checked:NO];
    [self setBtnChecked:self.yun checked:NO];
    [self setBtnChecked:self.men checked:NO];
    
    [self performSelector:@selector(requestSymptom) withObject:nil afterDelay:0.1];
    
    self.kun.target = self;
    self.kun.action = @selector(onBtnClick:);
    
    self.fa.target = self;
    self.fa.action = @selector(onBtnClick:);
    
    self.yun.target = self;
    self.yun.action = @selector(onBtnClick:);
    
    self.men.target = self;
    self.men.action = @selector(onBtnClick:);
}

-(void) requestSymptom
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    GlobalState* state = [GlobalState sharedInstance];
    NSString* uid = state.uid;
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetSymptom:)];
    [protocol getAllSymptom:uid];
}

-(void) onGetSymptom:(NSString*) json
{
    NSArray* ary = [json JSONValue];

    GlobalState* state = [GlobalState sharedInstance];
    for(NSDictionary* symptomInfo in ary)
    {
        NSString* symptomType = [symptomInfo objectForKey:@"symptomType"];
        NSString* symptomName = [state mapSymptomCode2Name:symptomType];
        
        NSRange range;
        range = [symptomName rangeOfString:@"困"];
        if (range.location != NSNotFound) {
            [self setBtnChecked:self.kun checked:YES];
        }
        
        
        range = [symptomName rangeOfString:@"乏"];
        if(range.location != NSNotFound){
            [self setBtnChecked:self.fa checked:YES];
        }
        
        range = [symptomName rangeOfString:@"晕"];
        if(range.location != NSNotFound){
            [self setBtnChecked:self.yun checked:YES];
        }
        
        range = [symptomName rangeOfString:@"闷"];
        if(range.location != NSNotFound){
            [self setBtnChecked:self.men checked:YES];
        }
    }
    
}

-(void) setBtnChecked:(UIBarButtonItem*) btn checked:(BOOL) checked
{
    if(checked){
        btn.tintColor = [UIColor blueColor];
        btn.tag = true;
    }else
    {
        btn.tintColor = [UIColor grayColor];
        btn.tag = false;
    }
    
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

    [center postNotificationName:BOTTOM_BAR_TAP object:self userInfo:nil];
}


-(void) onBtnChecked:(UIBarButtonItem*) btn checked:(BOOL) b
{
    NSString* symptomName = [btn valueForKey:@"title"];
    
    GlobalState* state = [GlobalState sharedInstance];
    
    NSString* symptomType = [state mapSymptomName2Code:symptomName];
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    if(b){
        [protocol addSymptom:state.uid symptomType:symptomType shared:@"false"];
    }
    else{
        [protocol deleteSymptom:state.uid symptomType:symptomType];
    }

}

-(void) onBtnClick:(id) sender
{
    UIBarButtonItem* btn = (UIBarButtonItem*) sender;

    if(btn.tag){
        [self setBtnChecked:btn checked:NO];
        [self onBtnChecked:btn checked:NO];
    }else {
        [self setBtnChecked:btn checked:YES];
        [self onBtnChecked:btn checked:YES];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.kun = nil;
    self.fa = nil;
    self.yun = nil;
    self.men = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



@end
